package com.xc.controller;

import com.xc.common.ServerResponse;
import com.xc.service.IUserService;
import com.xc.utils.PropertiesUtil;
import com.xc.utils.redis.CookieUtils;
import com.xc.utils.redis.JsonUtil;
import com.xc.utils.redis.RedisConst;
import com.xc.utils.redis.RedisShardedPoolUtils;
import com.xc.utils.sms.ali.AliyunSmsConst;
import com.xc.vo.user.UserLoginResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * @author mipha
 * @version 1.0
 * @date 2019/2/15 3:06 PM
 */
@Controller
@RequestMapping("/api/user/")
@Slf4j
public class UserApiController {

    @Autowired
    IUserService iUserService;

    /**
     *  用户注册
     * @param agentCode
     * @param phone
     * @param yzmCode
     * @param userPwd
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "reg.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse reg(@RequestParam("agentCode") String agentCode,
                              @RequestParam("phone") String phone,
                              @RequestParam(value = "yzmCode",defaultValue = "") String yzmCode,
                              @RequestParam("userPwd") String userPwd,
//                              @RequestParam("realName") String realName,
                              HttpServletRequest httpServletRequest){

        ServerResponse serverResponse = iUserService.reg(yzmCode,agentCode,phone,userPwd,httpServletRequest);
        return serverResponse;
    }

    /**
     *  用户登陆
     * @param phone
     * @param userPwd
     * @param httpSession
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse login(@RequestParam("phone") String phone,
                                @RequestParam("userPwd") String userPwd,
                                HttpSession httpSession,
                                HttpServletRequest request,
                                HttpServletResponse response){

        String pc_cookie_name = PropertiesUtil.getProperty("user.cookie.name");
        String token = RedisConst.getUserRedisKey(httpSession.getId());

        ServerResponse serverResponse = iUserService.login(phone,userPwd,request);
        if(serverResponse.isSuccess()){  //  登陆成功
            //  往浏览器发送一个logintoken
            CookieUtils.writeLoginToken(response
                    , token , pc_cookie_name);
            //  在redis服务器上保存用户的登陆信息
            String redisSetExResult = RedisShardedPoolUtils
                    .setEx(token , JsonUtil.obj2String(serverResponse.getData()),
                            RedisConst.RedisCacheExTime.REDIS_SESSION_EXTIME);
            log.info("redis setex user result : {}",redisSetExResult);

            UserLoginResultVO resultVO = new UserLoginResultVO();
            resultVO.setKey(pc_cookie_name);
            resultVO.setToken(token);
            return ServerResponse.createBySuccess("登陆成功",resultVO);
        }else{
            return serverResponse;
        }
    }

    /**
     *  用户注销登陆
     * @param httpServletRequest
     * @param httpServletResponse
     * @return
     */
    @RequestMapping("logout.do")
    @ResponseBody
    public ServerResponse logout(HttpServletRequest httpServletRequest,
                                 HttpServletResponse httpServletResponse){
        String cookie_name = PropertiesUtil.getProperty("user.cookie.name");
        String logintoken = CookieUtils.readLoginToken(httpServletRequest,cookie_name);
        log.info("用户 token = {} ,退出登陆",logintoken);
        //  删除redis服务器上的logintoken
        RedisShardedPoolUtils.del(logintoken);
        //  删除cookie
        CookieUtils.delLoginToken(httpServletRequest,httpServletResponse,cookie_name);
        return ServerResponse.createBySuccess();
    }


    /**
     *  检查手机号是否注册
     * @param phoneNum 手机号
     *                 存在返回 success
     *                 不存在返回 fail
     * @return
     */
    @RequestMapping("checkPhone.do")
    @ResponseBody
    public ServerResponse checkPhone(String phoneNum){
        return iUserService.checkPhone(phoneNum);
    }


    /**
     *  非登陆状态下通过手机验证短信修改密码
     * @param phoneNum
     * @param code
     * @param newPwd
     * @return
     */
    @RequestMapping("updatePwd.do")
    @ResponseBody
    public ServerResponse updatePwd(String phoneNum,String code,String newPwd){
        return iUserService.updatePwd(phoneNum,code,newPwd);
    }





}
